package com.aplink.generic.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckedTextView;

import com.aplink.generic.pojo.GenericObject;
import com.aplink.sg.R;

public class LanguageAdapter extends BaseAdapter {
	static class ViewHolder {
		CheckedTextView mLanguageName;
	}
	private static final String LANGUAGE_CODE = "Language.InternationalCode";
	private static final String LANGUAGE_ID = "Language.LanguageID";
	private static final String LANGUAGE_NAME = "Language.LanguageName";
	private Context mContext;

	private ArrayList<GenericObject> mLanguages;

	public LanguageAdapter(Context context, ArrayList<GenericObject> objects) {
		mLanguages = objects;
		setContext(context);
	}

	public Context getContext() {
		return mContext;
	}

	@Override
	public int getCount() {
		return mLanguages.size();
	}

	@Override
	public GenericObject getItem(int position) {
		return mLanguages.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	public String getLanguageCode(int position) {
		return getItem(position).getValue(LANGUAGE_CODE);
	}

	public String getLanguageID(int position) {
		return getItem(position).getValue(LANGUAGE_ID);
	}

	public int getPosition(String languageID) {
		for (GenericObject genericObject : mLanguages) {
			if (genericObject.getValue(LANGUAGE_ID)
					.equalsIgnoreCase(languageID)) {
				return mLanguages.indexOf(genericObject);
			}
		}
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		if (convertView == null) {
			LayoutInflater layoutInflater = (LayoutInflater) getContext()
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = layoutInflater.inflate(R.layout.language_item,
					parent, false);
			CheckedTextView languagename = (CheckedTextView) convertView
					.findViewById(R.id.textview_language_name);
			viewHolder = new ViewHolder();
			viewHolder.mLanguageName = languagename;
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		viewHolder.mLanguageName.setText(mLanguages.get(position).getValue(
				LANGUAGE_NAME));
		return convertView;
	}

	public void setContext(Context context) {
		mContext = context;
	}
}
